import Utils from "../../lib/Utils";
import ItemBaseData from "./ItemBaseData";

export default class GateData{
    public id:number;
    public itemLen:number;
    public items:string;
    public step:number;
    public star1:number=0;
    public star2:number=0;
    public star3:number=0;
    /**0消除泡泡 1消除钻石 2消除彩色钻石*/
    public task:number=0;
    public itemvos:Array<{id:number,row:number,col:number}>=[];
    public maxRow:number=0;

    constructor(_cfg:string=null){
        if(_cfg)this.parse(_cfg);
    }

    public parse(_cfg:string){
        let tdata = JSON.parse(_cfg);
        this.parse2(tdata);
    }

    public parse2(_data){
        Utils.copyObjAtt(this,_data);
        this.parseitems(this.items);
       
    }

    public initStarScore(totalScore:number){
        if(!this.star1){
            this.star1 = Math.floor(totalScore*0.3);
            this.star2 = Math.floor(totalScore*0.7);
            this.star3 = Math.floor(totalScore*0.9);
        }
    }

    public getGateName(){
        return "第"+this.id+"关";
    }

    public getTaskStar(_score:number){
        let tstar=0;
        if(_score>=this.star3)tstar=3;
        else if(_score>=this.star2)tstar=2;
        else if(_score>=this.star1)tstar=1;
        return tstar;
    }

    public getTaskDes(){
        let str="消除泡泡";
        if(this.task==1)str="消除钻石";
        else if(this.task==2)str="消除钻石";
        return str;
    }

    public getTaskIcon(){
        let ticon="main/t22.png";
        if(this.task==1)ticon="main/t1.png";
        else if(this.task==2)ticon="main/t3.png";
        return ticon;
    }

    private parseitems(_value:string){
        let ttarr = _value.split("|");
        this.itemvos=[];
        let temp=[];
        let tdata;
        for(let i=0;i<ttarr.length;i++){
            temp=ttarr[i].split("_");
            tdata = {id:parseInt(temp[0]),row:parseInt(temp[1]),col:parseInt(temp[2])}
            if(this.maxRow<tdata.row+1)this.maxRow=tdata.row+1
            this.itemvos.push(tdata);
        }
    }

    // private parseProps(_value:string){
    //     this.props=[];
    //     if(_value){
    //         let ttarr = _value.split("|");
    //         let tvo:ItemBaseData;
    //         for(let i=0;i<ttarr.length;i++){
    //             tvo=new ItemBaseData();
    //             tvo.setDataById(ttarr[i]);
    //             this.props.push(tvo);
    //         }

    //     }
    // }
}